
package horde.minefield.andres.robots;

import horde.interfaces.Requests;
import horde.patterns.Msg;
import horde.internalResources.Directory;
import horde.internalResources.HorrorControl.HorrorControl;
import horde.standard.resources.graphics.HFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.DefaultTableModel;

/**
 * @author Andres
 */
public class robotControl extends JFrame implements ActionListener, WindowListener {
    
    private DefaultTableModel tableModel;
    private JTable table;
    private JButton sendWork, getWorkers;
    private HorrorControl hc = HorrorControl.getInstance();

    public robotControl () {
        /* Use nimbus */
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {}
        hc.getFrameInstance();
//        hc.setVisibleFrame();
        /* Constructor code */
        this.setSize(new Dimension (450,300));
        this.setLocationRelativeTo(null);
        this.setTitle("Robots");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        tableModel = new DefaultTableModel();
        tableModel.addColumn("Robot");
        tableModel.addColumn("Status");
        tableModel.addColumn("Talk");
        this.setLayout(new BorderLayout(2,2));
        sendWork = new JButton("Send work");
        sendWork.addActionListener(this);
        getWorkers = new JButton ("Get Workers");
        getWorkers.addActionListener(this);
        table = new JTable (tableModel);
        add (new JScrollPane(table), BorderLayout.CENTER);
        add (sendWork, BorderLayout.PAGE_END);
        add (getWorkers, BorderLayout.PAGE_START);
        this.addWindowListener(this);
        setVisible(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sendWork) {
            int number_of_robots = tableModel.getRowCount();
            Msg msg = Msg.composeForGroup("robotControl", "default", "work", new Integer(number_of_robots));
            Requests rq = Directory.getInstance().getRequests();
            rq.post(msg);
        }
        else if (e.getSource() == getWorkers) {
            Msg msg = Msg.composeForGroup("robotControl", "default", "start");
            Directory.getInstance().getRequests().post(msg);
        }
    }

    public void login (String id, String status, String talk) {
        String [] rowData = {id,status,talk};
        tableModel.addRow(rowData);
        /* Sends a message to confirm agent login */
        Msg msg = Msg.composeRequest("robotControl", id, "logged");
        Directory.getInstance().getRequests().post(msg);
    }

    public void report (String id, String status, String talk) {
        boolean found = false;
        int size = tableModel.getRowCount();
        int iterator = 0;
        while (!found && iterator < size) {
            String current_id = (String)tableModel.getValueAt(iterator, 0);
            if (current_id.equals(id)) {
                tableModel.setValueAt(status, iterator, 1);
                tableModel.setValueAt(talk, iterator, 2);
                found = true;
            } else {
                iterator++;
            }
        }
    }

    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {
        Directory.getInstance().getRequests().halt();
    }

    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
}
